package com.atguigu.order.service.impl;

import com.atguigu.order.entity.OrderEntity;
import com.atguigu.order.entity.OrderReturnReasonEntity;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.order.dao.OrderItemDao;
import com.atguigu.order.entity.OrderItemEntity;
import com.atguigu.order.service.OrderItemService;

@RabbitListener(queues={"hello.java.queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

 /*   @RabbitListener(queues={"hello.java.queue"})
    public void recieveMessage(Message message, OrderReturnReasonEntity content, Channel channel) throws InterruptedException {
        //{"id":1,"name":"张三","sort":null,"status":null,"createTime":1671544361138}
        byte[] body = message.getBody();
        //[headers={__TypeId__=com.atguigu.order.entity.OrderReturnReasonEntity}, contentType=application/json, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=hello.java.directExchange, receivedRoutingKey=helloQueue, deliveryTag=2, consumerTag=amq.ctag-Jt7V1pI0SOv9QroHM48f5w, consumerQueue=hello.java.queue])
        MessageProperties properties = message.getMessageProperties();
        // System.out.println("接收到消息...内容：" + message + "==>类型：" + message.getClass());
        //接收到消息...内容：OrderReturnReasonEntity(id=1, name=张三, sort=null, status=null, createTime=Tue Dec 20 22:29:31 CST 2022)
        System.out.println("接收到消息...内容："+ content);
        Thread.sleep(3000);
        System.out.println("消息处理完成==>："+ content.getName());
    }*/


    @RabbitHandler
    public void recieveMessage(Message message, OrderReturnReasonEntity content, Channel channel) throws InterruptedException {
        byte[] body = message.getBody();
        MessageProperties properties = message.getMessageProperties();

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("接收到消息....内容==>："+ content.getName());

        try {
            if (deliveryTag%2==0) {
                channel.basicAck(deliveryTag, false);
                System.out.println("签收了货物........" + deliveryTag);
            }else{
                /**
                 * requeue为true重新入队,false不入队并丢弃
                 */
                channel.basicNack(deliveryTag, false, false);
                System.out.println("没有签收货物........" + deliveryTag);
            }
        } catch (IOException e) {
            //网络异常
        }

    }

    @RabbitHandler
    public void recieveMessage(OrderEntity content) throws InterruptedException {
        System.out.println("接收到消息....内容==>："+ content.getOrderSn());
    }

}